﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Data;
using DTOService;

namespace DAOService
{
    public class DAOTinhTrang : IDAOTinhTrang
    {
        Provider p = new Provider();
        #region IDAOTinhTrang Members

        public TinhTrang[] SelectTinhTrang()
        {
            TinhTrang[] rs;
            try
            {
                DataTable dt = p.ExecuteStoreProcedure("sp_DanhSachTinhTrang");
                rs = new TinhTrang[dt.Rows.Count];
                TinhTrang temp;
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    temp = new TinhTrang();
                    temp.MaTinhTrang = int.Parse(dt.Rows[i][0].ToString());
                    temp.TenTinhTrang = dt.Rows[i][1].ToString();
                    rs[i] = temp;
                }
            }
            catch (Exception ex)
            { 
                throw new Exception("Error: " + ex.Message); 
            }
            return rs;
        }
        public TinhTrang GetTinhTrang(int ma)
        {
            TinhTrang temp = new TinhTrang();
            try
            {
                DataTable dt = p.ExecuteStoreProcedure("sp_GetTinhTrang", new string[] { "@ma" }, new object[] { ma });
                if (dt.Rows.Count != 0)
                {
                    temp.MaTinhTrang = int.Parse(dt.Rows[0][0].ToString());
                    temp.TenTinhTrang = dt.Rows[0][1].ToString();
                }
            }
            catch (Exception ex)
            { throw new Exception(ex.Message); }
            return temp;
        }

        public int InsertTinhTrang(TinhTrang parr)
        {
            try
            {
                Provider p = new Provider();
                DataTable dt = p.ExecuteStoreProcedure("sp_ThemTinhTrang", new string[] { "tentinhtrang" }, new object[] { parr.TenTinhTrang });
                return int.Parse(dt.Rows[0][0].ToString());
            }
            catch
            {
                return 0;
            }
        }

        public int UpdateTinhTrang(TinhTrang parr)
        {
            try
            {
                Provider p = new Provider();
                DataTable dt = p.ExecuteStoreProcedure("sp_CapNhatTinhTrang", new string[] { "matinhtrang", "tentinhtrang" }, new object[] {parr.MaTinhTrang, parr.TenTinhTrang });
                return int.Parse(dt.Rows[0][0].ToString());
            }
            catch
            {
                return 0;
            }
        }

        public int DeleteTinhTrang(TinhTrang parr)
        {
            try
            {
                Provider p = new Provider();
                DataTable dt = p.ExecuteStoreProcedure("sp_XoaTinhTrang", new string[] { "matinhtrang" }, new object[] { parr.MaTinhTrang });
                return int.Parse(dt.Rows[0][0].ToString());
            }
            catch
            {
                return 0;
            }
        }
        #endregion
    }
}
